What is @stablelib/constant-time?
@stablelib/constant-time is a JavaScript library that provides functions for performing constant-time operations. These operations are designed to take the same amount of time regardless of the input values, which is crucial for cryptographic applications to prevent timing attacks.
Constant-time string comparison
This feature allows you to compare two strings in constant time, which is useful for preventing timing attacks that could exploit the time it takes to compare different strings.
const { equal } = require('@stablelib/constant-time');
const a = 'hello';
const b = 'hello';
const c = 'world';
console.log(equal(a, b)); // true
console.log(equal(a, c)); // false
Constant-time byte array comparison
This feature allows you to compare two byte arrays in constant time, which is useful for cryptographic operations where you need to ensure that the comparison does not leak information through timing.
const { equal } = require('@stablelib/constant-time');
const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([1, 2, 3]);
const c = new Uint8Array([4, 5, 6]);
console.log(equal(a, b)); // true
console.log(equal(a, c)); // false